<?php
/**
 * Created by PhpStorm.
 * User: fengyan
 * Date: 17-11-3
 * Time: 下午3:39
 */

namespace App\Repositories\Basic;


use App\Repositories\Basic\BasicRepository;
use App\Models;
class LinkRepository extends BasicRepository
{
    public function __construct()
    {
        parent::__construct();
        $this->model = new Models\Basic\Link();
    }

    private static $instance;

    /**
     * 实现单例
     */
    public static function getInstance()
    {
        if (!(self::$instance instanceof self)) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    /**
     * 声明实例模型
     * @param array $filter
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function getSelectModel(array $filter = []): \Illuminate\Database\Eloquent\Builder
    {
        $model = $this->model;

        $model = $this->getSiteIdModel($model, $filter);

        //  状态
        if (isset($filter['status'])) {
            $model = $model->where('status', '=', (int) $filter['status']);
        }

        $model = $model->where('deleted_at', null);

        return $model;
    }

    /**
     * 后台分页
     * @param null $siteId
     * @param int $status
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getAdminPaginateData($siteId = null, $status = null): \Illuminate\Contracts\Pagination\LengthAwarePaginator
    {
        return $this->getSelectModel([
            'site_id' => $siteId,
            'status' => $status,
        ])->paginate($this->pageSize);
    }

    /**
     * 获取列表
     * @param null $siteId
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function getLinkList($siteId = null)
    {
        return $this->getSelectModel([
            'site_id' => $siteId,
            'status' => 99,
        ])->orderBy('sort', 'asc')->orderBy('id', 'asc')->get();
    }
}